<%@ page language="java" pageEncoding="UTF-8" import="com.chinapost.util.AuthUtil"%>
<%@ taglib prefix="my" uri="/WEB-INF/tlds/permission.tld" %>
<%
	AuthUtil authUtil = new AuthUtil(session);
%>
<script type="text/javascript">
var user_datagrid;
$(function ()
{
    user_datagrid = $('#user_manager_datagrid').datagrid(
        {
            url : '${pageContext.request.contextPath}/userAction!datagrid.action',
            pagination : true,
            pageSize : 20,
            pageList :  [  10, 20, 30, 40, 50 ],
            border : false,
            fit: true,
            fitColumns: true,
            idField : 'cid',
            sortName : 'cmodifydate',
            sortOrder : 'asc',
            checkOnSelect : false,
            selectOnCheck : true,
            columns : [[
                    {
                        title : 'id',
                        field : 'cid',
                        width : 100,
                        align : "center",
                        checkbox:true
                    },
                    {
                        title : '机构名称',
                        field : 'deptname',
                        align : "center",
                        width : 130
                    },
                    {
                        title : '用户编号',
                        field : 'cbh',
                        align : "center",
                        width : 80
                    },
                    {
                        title : '用户名',
                        field : 'cname',
                        align : "center",
                        width : 80
                    },
                    {
                        title : '员工类别',
                        field : 'yglbname',
                        align : "center",
                        width : 80
                    },
                    {
                        title : '用户角色',
                        field : 'roleNames',
                        align : "center",
                        width : 100
                    },
                    {
                        title : '创建时间',
                        field : 'ccreatedate',
                        align : "center",
                        width : 80
                    },
                    {
                        title : '修改时间',
                        field : 'cmodifydate',
                        align : "center",
                        width : 80
                    },
                    {
                        title : '上次登陆',
                        field : 'clastlogin',
                        align : "center",
                        width : 80
                    },
                    {
                        title : '登录次数',
                        field : 'clogin',
                        align : "center",
                        width : 60
                    },
                    {
                        title : '用户状态',
                        field : 'cstate',
                        align : "center",
                        width : 80,
                        formatter : function(value, row, index) {
                        	if (value===1)
    						{
    							return '<span style="color:#00CC00">正常</span>';
    						}else{
    							return '<span style="color:#FF0000">停用</span>';
    						}
        				}
                    },
                    {
                        field : 'action',
                        title : '操作',
                        width : 120,
                        align : 'center',
                        formatter : function (value, row, index)
                        {
                        	var tip;
                        	var icon;
                        	if (row.cstate===0){
                        		tip='启用';
                        		icon='${pageContext.request.contextPath}/css/themes/icons/key.png';
                        	}else{
                        		tip='停用';
                        		icon='${pageContext.request.contextPath}/css/themes/icons/lock.png';
                        	}
                        	var str='';
                        	<%if (authUtil.isAuth("/userAction!userEdit.action")){%>
                        	str += sy.fs('<img onclick="userEdit(\'{0}\');" src="{1}" style="cursor: pointer;" title="修改"/>&nbsp;',row.cid,'${pageContext.request.contextPath}/css/themes/icons/pencil.png');
                        	<%}%>
                        	<%if (authUtil.isAuth("/userAction!delete.action")){%>
                        	str += sy.fs('<img onclick="userDelete(\'{0}\');" src="{1}" style="cursor: pointer;" title="删除"/>&nbsp;',row.cid,'${pageContext.request.contextPath}/css/themes/icons/no.png');
                        	<%}%>
                        	<%if (authUtil.isAuth("/userAction!changeStatus.action")){%>
                        	str += sy.fs('<img onclick="userChangeStatus(\'{0}\');" src="{1}" style="cursor: pointer;" title="{2}"/>&nbsp;',row.cid,icon,tip);
                        	<%}%>
                        	return str;
                        }
                    }
                ]],
            toolbar : '#user_toolbar'
        }
        );
    $('#user_manager_dept').val('${sessionInfo.deptid}');
}
);

function userEdit(cid)
{
    if (cid != undefined)
    {
        $('#user_manager_datagrid').datagrid('unselectAll');
        $('#user_manager_datagrid').datagrid('selectRecord', cid);
    }
    var rows = $('#user_manager_datagrid').datagrid('getSelected');
    var p = parent.sy.dialog(
        {
            title : '用户资料编辑',
            iconCls : 'icon-edit',
            href : '${pageContext.request.contextPath}/userAction!userEdit.action',
            width : 510,
            height : 300,
            buttons : [
                {
                    text : '保存',
                    iconCls : 'icon-save',
                    handler : function ()
                    {
                        var f = p.find('form');
                        f.form('submit',
                        {
                            url : '${pageContext.request.contextPath}/userAction!edit.action',
                            success : function (d)
                            {
                                var json = $
                                    .parseJSON(d);
                                if (json.success)
                                {
                                    user_datagrid.datagrid('reload');
                                    p.dialog('close');
                                }
                                parent.sy.messagerShow(
                                {
                                    msg : json.msg,
                                    title : '提示'
                                }
                                );
                            }
                        }
                        );
                    }
                }
            ],
            onLoad : function ()
            {
                var f = p.find('form');
                <% if (authUtil.isAdmin()) { %>
                var roleIds = f.find('input[name=roleIds]');
                var roleTree = roleIds.combobox({
					url : '${pageContext.request.contextPath}/roleAction!do_combobox.action',
					required:true,
					editable:false,
					panelHeight:'auto',
					valueField:'cid',  
					textField:'cname',
					checkbox : true,
					multiple:true,
					formatter: function (row) {
	                    var opts = $(this).combobox('options');
	                    return '<input type="checkbox" class="combobox-checkbox">' + row[opts.textField];
	                },
	                onLoadSuccess: function () {
	                    var opts = $(this).combobox('options');
	                    var target = this;
	                    var values = $(target).combobox('getValues');
	                    $.map(values, function (value) {
	                        var el = opts.finder.getEl(target, value);
	                        el.find('input.combobox-checkbox')._propAttr('checked', true);
	                    });
	                },
	                onSelect: function (row) {
	                    var opts = $(this).combobox('options');
	                    var el = opts.finder.getEl(this, row[opts.valueField]);
	                    el.find('input.combobox-checkbox')._propAttr('checked', true);
	                },
	                onUnselect: function (row) {
	                    var opts = $(this).combobox('options');
	                    var el = opts.finder.getEl(this, row[opts.valueField]);
	                    el.find('input.combobox-checkbox')._propAttr('checked', false);
	                }
            	});
                f.form('load',rows);
                roleIds.combo('setValues',sy.getList(rows.roleIds));
                <%}else{%>
                f.form('load',rows);
                <%}%>
                f.find('input[name="cpwd"]').val("").focus();
            }
        }
        );
};
function userAppend()
{
    var p = parent.sy.dialog(
        {
            title : '添加记录',
            iconCls : 'icon-append',
            href : '${pageContext.request.contextPath}/userAction!userAdd.action',
            width : 510,
            height : 300,
            buttons : [
                {
                    text : '保存',
                    iconCls : 'icon-save',
                    handler : function ()
                    {
                        var f = p.find('form');
                        f.form('submit',
                        {
                            url : '${pageContext.request.contextPath}/userAction!add.action',
                            success : function (d)
                            {
                                var json = $.parseJSON(d);
                                if (json.success)
                                {
                                    user_datagrid.datagrid('reload');
                                    p.dialog('close');
                                }
                                parent.sy.messagerShow(
                                {
                                    msg : json.msg,
                                    title : '提示'
                                }
                                );
                            }
                        }
                        );
                    }
                }
            ]
        }
        );
}
function userRemove()
{
    var rows = user_datagrid.datagrid('getChecked');
    var ids = [];
    if (rows.length > 0)
    {
        parent.sy.messagerConfirm('请确认', '是否删除当前所选记录？', function (r)
        {
            if (r)
            {
                for (var i = 0; i < rows.length; i++)
                {
                    ids.push(rows[i].cid);
                }
                $.ajax(
                {
                    url : '${pageContext.request.contextPath}/userAction!delete.action',
                    data :
                    {
                        ids : ids.join(',')
                    },
                    dataType : 'json',
                    success : function (d)
                    {
                        user_datagrid.datagrid('load');
                        $('#user_manager_datagrid').datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
                        parent.sy.messagerShow(
                        {
                            title : '提示',
                            msg : d.msg
                        }
                        );
                    }
                }
                );
            }
        }
        );
    }
    else
    {
        parent.sy.messagerAlert('提示', '请选择要删除的记录！', 'error');
    }
};

function userDelete(cid)
{
    $('#user_manager_datagrid').datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
    $('#user_manager_datagrid').datagrid('checkRow', $('#user_manager_datagrid').datagrid('getRowIndex', cid));
    userRemove();
}

function userChangeStatus(cid){
	$.ajax(
            {
                url : '${pageContext.request.contextPath}/userAction!changeStatus.action',
                data :
                {
                    cid : cid
                },
                dataType : 'json',
                success : function (d)
                {
                    user_datagrid.datagrid('reload');
                    $('#user_manager_datagrid').datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
                    parent.sy.messagerShow(
                    {
                        title : '提示',
                        msg : d.msg
                    }
                    );
                }
            });
}

function userImport() {
	var p = parent.sy.dialog({
		title : '导入用户数据',
		iconCls : 'icon-import',
		href : '${pageContext.request.contextPath}/userAction!userImport.action',
		width : 510,
		height : 300,
		buttons : [ {
			text : '导入',
			iconCls : 'icon-accept',
			handler : function() {
				$.ajaxFileUpload({
					url : 'uploadFileAction!upload.action',//用于文件上传的服务器端请求地址  
					secureuri : false,//一般设置为false  
					fileElementId : 'user_import_file',//文件上传空间的id属性  <input type="file" id="file" name="file" />  
					dataType : 'text',//返回值类型 json，text  
					success : function(d, status) //服务器成功响应处理函数  
					{
						var json = $.parseJSON(d);
						if (json.state) {
							$.messager.progress({
								text : '正在处理，请稍候...',
								interval : 100
							});
							$.post("userAction!addUsers.action", {
								filename : json.filename,
								deptid : $('#user_import_dept').combotree('getValue')
							}, function(rsp) {
								if (rsp.success) {
									user_datagrid.datagrid('reload');
									p.dialog('close');
								}
								$.messager.progress('close');
								parent.sy.messagerAlert('提示', rsp.msg, 'info');
							}, "JSON");
						} else {
							$.messager.progress('close');
							parent.sy.messagerShow({
								msg : json.msg,
								title : '提示'
							});
						}
					},
					error : function(data, status, e)//服务器响应失败处理函数  
					{
						parent.sy.messagerAlert('错误', e, 'error');
					}
				});
			}
		} ]
	});
};


function _searchUser() {
	user_datagrid.datagrid('load', sy.serializeObject($('#user_search_form')));
	user_datagrid.datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
};
function _cleanSearchUser() {
	$('#user_manager_dept').combotree('setValue','${sessionInfo.deptid}');
	$('#user_search_form  input[name="cname"]').val('');
	$('#user_search_form  input[name="cbh"]').val('');
	user_datagrid.datagrid('load', sy.serializeObject($('#user_search_form')));
	 $('#user_manager_datagrid').datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
};

function export_user(){
	url='${pageContext.request.contextPath}/userAction!exportExcel.action';
	 $('#user_search_form').attr('action',url);
    $('#user_search_form').submit();
};
</script>




<div class="easyui-layout" data-options="fit:true" style="overflow: hidden;">
	<div data-options="region:'north',title:'查询用户',iconCls:'icon-search'" style="height: 80px;padding:5px;overflow: hidden;">
		<div style="padding:5px">
			<form method="post" id="user_search_form" style="display:inline">
			<span>所属机构：</span>
			<input id="user_manager_dept" class="easyui-combotree" name="deptid" data-options="lines:true,url:'deptAction!do_combotree.action',panelHeight:280,required:true,editable:false,idField:'id',textField:'text',parentField:'pid'" style="width:180px"/>&nbsp;&nbsp;
				<span>　用户名：</span>
				<input type='text' name='cname' style="width:100px" />
				<span>　用户编号：</span>
				<input type='text' name='cbh' style="width:100px" />
			</form>
			<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-search'" onclick="_searchUser();">查询</a>&nbsp;
			<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-clear'" onClick="_cleanSearchUser();;">清空</a>
			<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-excel'" onClick="export_user();;">导出</a>
		</div>
	</div>
	<div data-options="region:'center',title:'用户列表',border:true,iconCls:'icon-role'" style="overflow: hidden;">
		<table id="user_manager_datagrid"></table>
	</div>
</div>
<div id="user_toolbar" style="display: none;" >
<table  class="toolbar" >
	<tr>
		<my:per url="/userAction!userAdd.action">
		<td><a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-append',plain:true" onclick="userAppend();">新增</a></td>
		<td><div class="datagrid-btn-separator"></div></td>
		</my:per>
		<my:per url="/userAction!delete.action">
		<td><a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-delete',plain:true" onclick="userRemove()">批量删除</a></td>
		<td><div class="datagrid-btn-separator"></div></td>
		</my:per>
		<my:per url="/userAction!userImport.action">
		<td><a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-import',plain:true" onclick="userImport()">批量导入</a></td>
		<td><div class="datagrid-btn-separator"></div></td>
		</my:per>
	</tr>
</table>
</div>
